<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="公寓楼号" prop="flatNo">
        <el-input v-model="dataForm.flatNo" placeholder="公寓楼号"></el-input>
      </el-form-item>
      <el-form-item label="楼层数" prop="floorNum">
        <el-input v-model="dataForm.floorNum" placeholder="楼层数"></el-input>
      </el-form-item>
      <el-form-item label="房间数" prop="roomNum">
        <el-input v-model="dataForm.roomNum" placeholder="房间数"></el-input>
      </el-form-item>
      <el-form-item label="男生(女生)宿舍楼标志" prop="flag">
        <el-input v-model="dataForm.flag" placeholder="男生(女生)宿舍楼标志"></el-input>
      </el-form-item>
      <el-form-item label="开始使用时间" prop="beginUseTime">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="dataForm.beginUseTime"
          style="width: 100%"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      // 数据
      visible: false,
      dataForm: {
        id: 0,
        flatNo: "",
        floorNum: "",
        roomNum: "",
        flag: 0,
        beginUseTime: "",
      },
      dataRule: {
        flatNo: [
          { required: true, message: "公寓楼号不能为空", trigger: "blur" },
        ],
        floorNum: [
          { required: true, message: "楼层数不能为空", trigger: "blur" },
        ],
        roomNum: [
          { required: true, message: "房间数不能为空", trigger: "blur" },
        ],
        beginUseTime: [
          {
            required: true,
            message: "开始使用时间不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  components: {
    // 组件注册
  },
  methods: {
    //表单提交
    dataFormSubmit() {
      console.log("submit");
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/apartment/buildinginfo/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              flatNo: this.dataForm.flatNo,
              floorNum: this.dataForm.floorNum,
              roomNum: this.dataForm.roomNum,
              flag: this.dataForm.flag,
              beginUseTime: this.dataForm.beginUseTime,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    //组件初始化方法
    init(myid) {
      this.dataForm.id = myid || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/apartment/buildinginfo/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.flatNo = data.buildingInfo.flatNo;
              this.dataForm.floorNum = data.buildingInfo.floorNum;
              this.dataForm.roomNum = data.buildingInfo.roomNum;
              this.dataForm.flag = this.dataForm.flag,
              this.dataForm.beginUseTime = data.buildingInfo.beginUseTime;
            }
          });
        }
      });
    },
  },
};
</script>